package com.czz.oss.controller;

import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.oss.service.FileUploadService;
import com.czz.oss.utils.ConstantPropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.POST;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: FileUploadTeacherController
 * @Author Czz
 * @Date 2020/1/21
 * @Time 15:37
 **/
@RestController
@RequestMapping(value = "/teacher/file")
public class FileUploadTeacherController {

    @Autowired
    FileUploadService fileUploadService;

    /**
    * @Description 上传文件至阿里云oss
    * @Param [file, host, courseId, chapterId]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/30
    * @Time 18:25
    **/
    @PostMapping(value = "/upload")
    public R fileUpload(
            @RequestParam(value = "file") MultipartFile file,
            @RequestParam(value = "host",required = false) String host,
            @RequestParam(value = "courseId", required = false) String courseId
    ){
        if (!StringUtils.isEmpty(host)){
            ConstantPropertiesUtil.FILE_HOST = host;
        }
        Map data = fileUploadService.uploadFile(file,courseId);
        return R.ok().message("文件上传成功").data("item",data);
    }

    /**
    * @Description 根据文件路径删除阿里云oss中文件
    * @Param
    * @Return
    * @Author Czz
    * @Date 2020/1/30
    * @Time 18:29
    **/
    @DeleteMapping(value = "/delete/{docUrl}")
    public R deleteOssFile(@PathVariable String docUrl){
        if (StringUtils.isEmpty(docUrl)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        fileUploadService.deleteFile(docUrl);
        return R.ok().message("文件删除成功");
    }

    /**
    * @Description 根据文件路径列表批量删除阿里云oss中文件
    * @Param [files]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/1
    * @Time 19:45
    **/
    @DeleteMapping(value = "/delete-batch")
    public R deleteOssFileBatch(@RequestBody List<String> files){
        if (CollectionUtils.isEmpty(files)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        fileUploadService.deleteFileBatch(files);
        return R.ok().message("批量文件删除成功");
    }

    /**
    * @Description 富文本编辑器上传图片
    * @Param []
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/3/9
    * @Time 11:53
    **/
    @PostMapping(value = "/tinymce")
    public R uploadEmpty(){
        return R.ok();
    }
}
